<template>
<div class="address-edit">

    <van-nav-bar
        title="修改收货地址"
        left-text="返回"
        left-arrow
        @click-left="$router.push('/address')"
    />
    <van-address-edit
        :area-list="areaList"
        :address-info="addressInfo"
        show-search-result
        :search-result="searchResult"
        @save="onSave"
        @change-detail="onChangeDetail"
    />

</div>
</template>

<script>
import {
    Toast
} from 'vant';
import areaList from '@/components/include/area.json';

export default {
    data() {
        let validatePhone = (rule, value, callback) => {
            if (!value) {
                return callback(new Error("手机号不能为空"));
            } else if (!/^1[3-9]{1}[0-9]{9}$/.test(value)) {
                return callback(new Error("手机号不正确"));
            } else {
                callback();
            }
        };
        return {
            areaList,
            searchResult: [],
            addressInfo: {
                name: '',
                tel: '',
                area_code: '',
                address_detail: '',
                postal_code: ''
            }
        };
    },
    computed: {
        itemIndex() {
            return this.$route.query.index;
        },
        newAddForm() {
            return this.$store.state.address[this.itemIndex];
        }
    },
    created() {
        this.loadItems()
    },
    methods: {
        onSave(content) {
            // Toast("save");
            content.address_id = this.$route.query.addressId


            this.apiPost('/shop.address/create', content).then(res => {


              this.$EventBus.$emit('address_update_success', 'ok')

              this.$router.push("/address");


            });



        },
        onDelete() {
            Toast("delete");
        },
        onChangeDetail(val) {
            if (val) {
                this.searchResult = [];
            } else {
                this.searchResult = [];
            }
        },
        loadItems() {
            // alert('dddd')
            this.apiPost('/shop.address/read', {
                address_id: this.$route.query.addressId
            }).then(res => {


              this.addressInfo.name = res.data.name
              this.addressInfo.tel = res.data.phone
              this.addressInfo.areaCode = res.data.area_code + ''
              this.addressInfo.addressDetail = res.data.detail + ''
              this.addressInfo.postalCode = res.data.postal_code + ''
              this.addressInfo.isDefault = res.data.default + ''


            });






        },
        toProfile() {
            this.$router.push({
                path: "/profile/edit_address_page"
            });
            //解决主页与详情页切换时，菜单栏无法更新获取当前路由的问题
            this.$store.state.cartCounter++;
            this.$store.state.cartCounter--;
        },
        submitForm(formName) {
            this.$refs[formName].validate(valid => {
                if (valid) {
                    this.$store.commit("modifyAddress", {
                        index: this.itemIndex,
                        value: this.newAddForm
                    });
                    this.$router.push({
                        path: "/profile/edit_address_page"
                    });
                } else {
                    return false;
                }
            });
        },
        deleteAddress() {
            this.$store.commit("deleteAddress", this.itemIndex);
            this.$router.push({
                path: "/profile/edit_address_page"
            });
        }
    }
};
</script>
